package vip.huhaiong.hlfood.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.huhaiong.hlfood.pojo.Rs;
import vip.huhaiong.hlfood.service.ListService;
import vip.huhaiong.hlfood.utils.JsoupUtil;
import vip.huhaiong.hlfood.utils.RedisUtil;
import vip.huhaiong.hlfood.utils.RsUtil;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ListServiceImpl implements ListService {
    JsoupUtil jsoupUtil;
    RedisUtil redisUtil;
    @Autowired
    public ListServiceImpl(JsoupUtil jsoupUtil, RedisUtil redisUtil){
        this.jsoupUtil = jsoupUtil;
        this.redisUtil = redisUtil;
    }
    @Override
    public Rs getIndexData(String url) throws Exception {
        Rs redisResult = redisUtil.get("index");
        if(redisResult == null){
            log.info("通过爬虫进行加载");
            Document document = jsoupUtil.getHtmlString(url);
            List<Map<String, Object>> indexData = jsoupUtil.getIndexData(document);
            Rs result = RsUtil.success("查询成功", indexData);
            redisUtil.set("index",result);
            return result;
        }else{
            log.info("通过redis进行加载");
            return redisResult;
        }
    }

    @Override
    public Rs getDetailData(String url) throws Exception {
        List<Map<String,Object>> list = jsoupUtil.getDataDetail(jsoupUtil.getHtmlString(url));
        return RsUtil.success("查询成功",list);
    }

    @Override
    public Rs getSearchData(String keywords) throws Exception {
        List<Map<String,Object>> list = jsoupUtil.getSearchData(keywords);
        return RsUtil.success("查询成功",list);
    }

    @Override
    public Rs getDataByType(String type, Integer page) throws Exception {
        List<Map<String, Object>> dataByType = jsoupUtil.getDataByType(type, page);
        return RsUtil.success("查询成功",dataByType);
    }

    @Override
    public Rs getVideoList(Integer page) throws Exception {
        List<Map<String, Object>> videoList = jsoupUtil.getVideoList(page);
        return RsUtil.success("查询成功",videoList);
    }

    @Override
    public Rs getVideoData(String url) throws Exception {
        List<Map<String, Object>> videoData = jsoupUtil.getVideoData(url);
        return RsUtil.success("查询成功",videoData);
    }
}
